package com.jackchan.homework.sysw.web;

import com.jackchan.homework.common.web.BaseController;
import com.jackchan.homework.sysw.entity.Recipe;
import com.jackchan.homework.sysw.entity.RecipeNeeds;
import com.jackchan.homework.sysw.service.RecipeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.List;
import java.util.Map;

/**
 * Created by ch-hui on 15/12/24.
 */
@Controller
@RequestMapping("recipe")
public class RecipeController extends BaseController {

    private static final String BIZ_BASE = "sysw/recipe/";

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RecipeService recipeService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView(BIZ_BASE + "recipe_list");
        mv.addObject("recipes", recipeService.findAllRecipeDtos());
        return mv;
    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public ModelAndView editForm(@PathVariable Long id) {
        ModelAndView mv = new ModelAndView(BIZ_BASE + "recipe_form");
        mv.addObject("recipeDto",recipeService.findOneRecipeDto(id));
        mv.addObject("recipe_mores",recipeService.queryRecipeReport(id));
        return mv;
    }


    @RequestMapping(value = "needs", method = RequestMethod.GET)
    public ModelAndView needs() {
        ModelAndView mv = new ModelAndView(BIZ_BASE + "recipe_needs");
        mv.addObject("recipe_needs", recipeService.findAllNeedsDto());
        return mv;
    }

    @RequestMapping(value = "needs/create", method = RequestMethod.GET)
    public String create() {
        return BIZ_BASE + "recipe_needs_form";
    }

    @RequestMapping(value = "needs/edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable Long id) {
        ModelAndView mv = new ModelAndView(BIZ_BASE + "recipe_needs_form");
        RecipeNeeds needs = recipeService.findOne(id);
        mv.addObject("needs", recipeService.findNutritionNeeds(needs));
        return mv;
    }

    @RequestMapping(value = "test/report", method = RequestMethod.GET)
    public String testReport(ModelMap model){

        logger.info("进入测试报表controller方法...");
        model.put("ReportName", "BirtReportTest.rptdesign");
        return "report";
    }


    /**
     * 查看配方单报表
     */
    @RequestMapping(value = "{id}/report", method = RequestMethod.GET)
    public String report(@PathVariable("id") Long id, ModelMap model,
                         HttpServletRequest request) {
        Recipe recipe = recipeService.findOneRecipe(id);
        if(recipe != null){
            model.put("ReportName", "RecipeReport.rptdesign");
            model.put("ReportFormat", "pdf");
            model.put("outputFileName", recipe.getRecipeName());
            // 报表参数
            request.setAttribute("reportName", recipe.getRecipeName());
            List<Map<String, Object>> dataSets = recipeService.queryRecipeReport(recipe.getId());
            request.setAttribute("report_data_set", dataSets);
            logger.info("报表数据集{}",dataSets.size());
        }
        return "report";
    }
}
